package com.cloudream.toucheventdemo.dispatch;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;

public class MyScrollView extends ScrollView {

    private String TAG = "MyScrollView";

    private int screenHeight = 0;

    private int headViewHeight = 0;

    private View headerView = null ;

    private ListView listView = null;

    public MyScrollView(Context context) {
        super(context);
        init_();
    }


    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init_();
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init_();
    }


    private void init_() {


        screenHeight = getResources().getDisplayMetrics().heightPixels;
        Log.d("MyScrollView", "screenHeight:" + screenHeight);
        headViewHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                100, getResources().getDisplayMetrics());

    }

    private boolean isNeedPastToChildView = false;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        View childAt = getChildAt(0);
        if (childAt instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) childAt;
            headerView = viewGroup.getChildAt(0);
            View childAt2 = viewGroup.getChildAt(1);
            if (childAt2 instanceof ListView) {
                listView = (ListView) childAt2;
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) childAt2.getLayoutParams();
                lp.height = screenHeight - headViewHeight;
                listView.setOnTouchListener(new OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {


                        Log.d(TAG,"onTouch" );
                        if(event.getAction() == MotionEvent.ACTION_MOVE && isNeedPastToChildView){
                            isNeedPastToChildView = false;
                            return true;
                        }

                        return false;
                    }
                });
            }


        }
    }


    private float lastY;


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean intercept = false;
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastY = ev.getY();
                super.onInterceptTouchEvent(ev);
                intercept = false;
                break;
            case MotionEvent.ACTION_MOVE:

                if(ev.getY() <=  headerView.getHeight()){
                    Log.d(TAG,"touch headview ");
                    intercept = true;
                }else if(ev.getY() < lastY && canScrollVertically(1)){
                    Log.d(TAG,"scroll up ,scrollView can scroll up  ");
                    intercept = true;
                }else if(!canScrollVertically(1) && ev.getY() < lastY){
                    Log.d(TAG,"scroll up ,scrollView can't scroll up");
                    intercept = false;
                }else if(ev.getY() > lastY && listView.canScrollVertically(-1)){
                    Log.d(TAG,"scroll down ,and listView can scroll down  ");
                    intercept = false;
                }else if(ev.getY() > lastY && !listView.canScrollVertically(-1)){
                    Log.d(TAG,"scroll dow ,and listView can't scroll down ");
                    intercept = true;
                }else{
                    Log.d(TAG,"ELSE ");

                }

                break;
            case MotionEvent.ACTION_UP:
                break;

        }
        lastY = ev.getY();


        return intercept;
    }




    private float mTouchLastY = 0;

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        isNeedPastToChildView = false;
        if(ev.getAction() == MotionEvent.ACTION_DOWN)
            mTouchLastY = ev.getY();
        else if(ev.getAction() == MotionEvent.ACTION_MOVE){
            Log.d("Terry","move y = "+ev.getY() +"lasty = "+mTouchLastY);
            if(!canScrollVertically(1)  && ev.getY() < mTouchLastY){
                Log.d("Terry","------!canScrollVertically------");
                isNeedPastToChildView = true;
                return false;

            }

        }

        mTouchLastY = ev.getY();
        return super.onTouchEvent(ev);
    }
}


